home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_c / cuj0696.zip / DWYER.ZIP / LIB / POLEVL.C < prev    next >
C/C++ Source or Header  |  1995-10-03  |  2KB  |  110 lines

  1. /*                            polevl.c
  2.  *                            p1evl.c
  3.  *
  4.  *    Evaluate polynomial
  5.  *
  6.  *
  7.  *
  8.  * SYNOPSIS:
  9.  *
  10.  * int N;
  11.  * double x, y, coef[N+1], polevl[];
  12.  *
  13.  * y = polevl( x, coef, N );
  14.  *
  15.  *
  16.  *
  17.  * DESCRIPTION:
  18.  *
  19.  * Evaluates polynomial of degree N:
  20.  *
  21.  *                     2          N
  22.  * y  =  C  + C x + C x  +...+ C x
  23.  *        0    1     2          N
  24.  *
  25.  * Coefficients are stored in reverse order:
  26.  *
  27.  * coef[0] = C  , ..., coef[N] = C  .
  28.  *            N                   0
  29.  *
  30.  *  The function p1evl() assumes that coef[N] = 1.0 and is
  31.  * omitted from the array.  Its calling arguments are
  32.  * otherwise the same as polevl().
  33.  *
  34.  *
  35.  * SPEED:
  36.  *
  37.  * In the interest of speed, there are no checks for out
  38.  * of bounds arithmetic.  This routine is used by most of
  39.  * the functions in the library.  Depending on available
  40.  * equipment features, the user may wish to rewrite the
  41.  * program in microcode or assembly language.
  42.  *
  43.  */
  44. /* -------------------------------------------------------------------- */
  45.  
  46. /*
  47. Cephes Math Library Release 2.1:  December, 1988
  48. Copyright 1984, 1987, 1988 by Stephen L. Moshier
  49. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  50. */
  51.  
  52.  
  53. # if defined(__STDC__) || defined(__PROTO__)
  54. double
  55. polevl(double x, double *coef, int N)
  56. # else
  57. double
  58. polevl(x, coef, N)
  59. double    x;
  60. double *coef;
  61. int    N;
  62. # endif
  63. {
  64. long double ans;
  65. int i;
  66. double *p;
  67.  
  68. p = coef;
  69. ans = *p++;
  70. i = N;
  71.  
  72. do
  73.     ans = ans * x  +  *p++;
  74. while( --i );
  75.  
  76. return( (double)ans );
  77. }
  78.  
  79. /*                            p1evl()    */
  80. /*                                          N
  81.  * Evaluate polynomial when coefficient of x  is 1.0.
  82.  * Otherwise same as polevl.
  83.  */
  84.  
  85. # if defined(__STDC__) || defined(__PROTO__)
  86. double
  87. p1evl(double x, double *coef, int N)
  88. # else
  89. double
  90. p1evl(x, coef, N)
  91. double    x;
  92. double *coef;
  93. int    N;
  94. # endif
  95. {
  96. long double ans;
  97. double *p;
  98. int i;
  99.  
  100. p = coef;
  101. ans = x + *p++;
  102. i = N-1;
  103.  
  104. do
  105.     ans = ans * x  + *p++;
  106. while( --i );
  107.  
  108. return( (double)ans );
  109. }
  110.